﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace imarks.Utility
{
    /// <summary>
    /// JSON传输结果类
    /// </summary>
    public class JsonTransfer
    {
        private Dictionary<string, object> dictionary = new Dictionary<string, object>();
        private string _errorKey = "error";
        private string _contentKey = "content";

        /// <summary>
        /// 
        /// </summary>
        public JsonTransfer()
        {

        }

        /// <summary>
        /// 默认追加到_contentKey里
        /// </summary>
        /// <param name="content"></param>
        public JsonTransfer(object content)
        {
            Add(_contentKey, content);
        }

        /// <summary>
        /// 追加响应键值对,value必须是可以序列化的对象
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public JsonTransfer Add(string key, object value)
        {
            if (value == null) //如果value=null，则不再输出到客户端
                return Remove(key);
            else
                dictionary[key] = value;
            return this;
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public JsonTransfer Remove(string key)
        {
            dictionary.Remove(key);
            return this;
        }

        /// <summary>
        /// 如果isJson=true，则直接添加一个json字符串到输出里，以json的形式输出
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="isJson"></param>
        /// <returns></returns>
        public JsonTransfer Add(string key, string value, bool isJson)
        {
            if (isJson)
                dictionary[key] = new RawJsonString(value);
            else
                dictionary[key] = value;
            return this;
        }

        /// <summary>
        /// 增加错误键
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public JsonTransfer SetError(object value)
        {
            return Add(_errorKey, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public JsonTransfer ClearError()
        {
            dictionary.Remove(_errorKey);
            return this;
        }

        /// <summary>
        /// 清空所有已经添加的对象
        /// </summary>
        public void Clear()
        {
            dictionary.Clear();
        }

        /// <summary>
        /// 结果以JSON格式输出
        /// </summary>
        /// <returns></returns>
        public string ToJson()
        {
            return ToJson(dictionary);
        }


        /// <summary>
        /// 结果以符合EasyUI框架的JSON格式输出
        /// </summary>
        /// <returns></returns>
        public string ToJsonForEasyUI(int total)
        {
            return ToJsonForEasyUI(dictionary, total);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(object obj)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.NullValueHandling = NullValueHandling.Ignore;
            settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.DefaultValueHandling = DefaultValueHandling.Ignore;
            settings.Converters.Add(new RawJsonStringConverter(typeof(RawJsonString)));

            return JsonConvert.SerializeObject(obj, Formatting.Indented, settings);//增加对rawjsonstring解析的支持
        }


        public static string ToJsonForEasyUI(object obj, int total)
        {
            return "{\"total\":" + total + ",\"rows\":" + ToJson(obj) + "}";
        }

        /// <summary>
        /// 将对象系列化字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SerializeObject(object value)
        {
            return JsonConvert.SerializeObject(value, Formatting.Indented);
        }

        /// <summary>
        /// 将字符串反系列化成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string value)
        {
            return JsonConvert.DeserializeObject<T>(value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return ToJson();
        }

        /// <summary>
        /// DataTable转JSON
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="dtName"></param>
        /// <returns></returns>
        public static string DataTableToJSON(DataTable dt, string dtName)
        {
            return JsonConvert.SerializeObject(dt, new DataTableConverter());
        }
    }
}
